package webbandidong.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import webbandidong.dao.CompanyDAO;
import webbandidong.model.Company;
import webbandidong.util.CustomHibernateDaoSupport;

@Repository(value = "companyDAOImpl")
public class CompanyDAOImpl extends CustomHibernateDaoSupport implements CompanyDAO {

    public List<Company> getAllCompanies() {
        Session session = null;
        Transaction transaction = null;

        try {
            session = getHibernateTemplate().getSessionFactory().openSession();
            transaction = session.beginTransaction();
            String hql = "from Company order by name";
            Query query = session.createQuery(hql);
            List<Company> companies = (List<Company>) query.list();
            transaction.commit();
            return companies;
        }
        catch (Exception ex) {
            try {
                transaction.rollback();
            }
            finally {
                return null;
            }
        }
    }
}
